﻿using System;
using System.ComponentModel;
using System.Linq;

namespace Microsoft.WowAddonStudio.FrameXml.Components.Support
{
    public static class ContainerExtensions
    {
        /// <summary>
        /// Finds an FrameXML control by its name.
        /// </summary>
        /// <typeparam name="TFrameXmlControl">The type of the FrameXML control.</typeparam>
        /// <param name="container">The container.</param>
        /// <param name="name">The name of the FrameXML control.</param>
        /// <returns>An instance of the <typeparamref name="TFrameXmlControl"/> class.</returns>
        public static TFrameXmlControl FindFrameXmlControlByName<TFrameXmlControl>(this IContainer container, string name) where TFrameXmlControl : IFrameXmlControl
        {
            if (container == null)
                throw new ArgumentNullException("container");
            if (name == null)
                throw new ArgumentNullException("name");

            return container.Components.OfType<TFrameXmlControl>()
                                       .SingleOrDefault(component => component.Name == name);
        }

        /// <summary>
        /// Finds an FrameXML control by its name.
        /// </summary>
        /// <param name="container">The container.</param>
        /// <param name="name">The name of the FrameXML control.</param>
        /// <returns>An instance of the <see cref="IFrameXmlControl"/> class.</returns>
        public static IFrameXmlControl FindFrameXmlControlByName(this IContainer container, string name)
        {
            return container.FindFrameXmlControlByName<IFrameXmlControl>(name);
        }
    }
}
